//
//  HJDiscoverSearchView.swift
//  WeiBo
//
//  Created by 韩俊 on 15/12/5.
//  Copyright © 2015年 HanJun. All rights reserved.
//

import UIKit

class HJDiscoverSearchView: UIView,UITextFieldDelegate {

    class func searchView() -> HJDiscoverSearchView {
        
        return NSBundle.mainBundle().loadNibNamed("HJDiscoverSearchView", owner: nil, options: nil).first as! HJDiscoverSearchView
    }
    
    @IBOutlet weak var rightLayout: NSLayoutConstraint!
    
    @IBOutlet weak var textField: UITextField!

    @IBOutlet weak var cencelBtn: UIButton!
    
    
    override init(frame: CGRect) {
        super.init(frame:frame)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override func awakeFromNib() {
        
        
//        self.textField.layer.cornerRadius = 5
//        self.textField.layer.borderColor = UIColor.redColor().CGColor
//        self.textField.layer.borderWidth = 2
        
        let leftView = UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))
        leftView.frame = CGRect(x: 0, y: 0, width: frame.height, height: frame.height)
        leftView.contentMode = .Center
        
        textField.leftView = leftView
        textField.leftViewMode = UITextFieldViewMode.Always
        
    }
    
    
    
    
    @IBAction func didCencel(sender: UIButton) {
        
        //取消textFiled第一响应者
        self.textField.resignFirstResponder()
        //第二种方法 取消textFiled第一响应者
        //self.endEditing(true)
        
        self.rightLayout.constant = 0
        UIView.animateWithDuration(0.25) { () -> Void in
            self.textField.layoutIfNeeded()
        }

    }

    
    // MARK: - UITextFieldDelegate
    func textFieldDidBeginEditing(textField: UITextField) {
        
        self.rightLayout.constant = self.cencelBtn.frame.width
        
        UIView.animateWithDuration(0.25) { () -> Void in
           self.textField.layoutIfNeeded()
        }
    }
    
}
